package com.iweb.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iweb.dto.AdminListInfo;
import com.iweb.page.DataGrid;
import com.iweb.utils.EntityUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.iweb.mapper.AdminMapper;
import com.iweb.model.Admin;
import com.iweb.service.AdminService;

import lombok.RequiredArgsConstructor;

import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class AdminServiceImpl implements AdminService {

    final private AdminMapper adminMapper;

    @Override
    public Admin findId(long id) throws Exception {
        return adminMapper.selectId(id);
    }

    @Override
    public Admin findPhone(String phone) throws Exception {

        return adminMapper.selectPhone(phone);
    }

    @Override
    public DataGrid<AdminListInfo> findList(int current, int size) throws Exception {
        DataGrid<AdminListInfo> dataGrid = new DataGrid<>();

        PageHelper.startPage(current, size); // 分页条件
        List<Admin> admins = adminMapper.selectList(); // 查询数据
        // 分页
        PageInfo<Admin> info = new PageInfo<>(admins);

        dataGrid.setPage(current);
        dataGrid.setPages(info.getPages());
        dataGrid.setTotal(info.getTotal());
        // 转一下数据
        List<AdminListInfo> list = EntityUtil.copyPropertiesToList(info.getList(), AdminListInfo.class);
        dataGrid.setList(list);

        return dataGrid;
    }

}
